package com.kxmall.web.controller.coupon;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import com.kxmall.common.annotation.Log;
import com.kxmall.common.annotation.RepeatSubmit;
import com.kxmall.common.core.controller.BaseController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.validate.AddGroup;
import com.kxmall.common.core.validate.EditGroup;
import com.kxmall.common.enums.BusinessType;
import com.kxmall.common.enums.CouponEnum;
import com.kxmall.common.exception.ServiceException;
import com.kxmall.common.utils.poi.ExcelUtil;
import com.kxmall.coupon.domain.bo.KxStoreCouponBo;
import com.kxmall.coupon.domain.vo.KxStoreCouponVo;
import com.kxmall.web.controller.coupon.service.IKxStoreCouponService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 优惠券
 *
 * @author kxmall
 * @date 2023-02-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/coupon/storeCoupon")
public class KxStoreCouponController extends BaseController {

    private final IKxStoreCouponService kxStoreCouponService;

    /**
     * 查询优惠券列表
     */
    @SaCheckPermission("coupon:storeCoupon:list")
    @GetMapping("/list")
    public TableDataInfo<KxStoreCouponVo> list(KxStoreCouponBo bo, PageQuery pageQuery) {
        return kxStoreCouponService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询所有有效优惠券
     */
    @SaCheckPermission("coupon:storeCoupon:list")
    @GetMapping("/listAll")
    public R<Map<String,Object>> listAll(KxStoreCouponBo bo) {
        return R.ok(kxStoreCouponService.queryAllForSelect(bo));
    }

    /**
     * 导出优惠券列表
     */
    @SaCheckPermission("coupon:storeCoupon:export")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KxStoreCouponBo bo, HttpServletResponse response) {
        List<KxStoreCouponVo> list = kxStoreCouponService.queryList(bo);
        ExcelUtil.exportExcel(list, "优惠券", KxStoreCouponVo.class, response);
    }

    /**
     * 获取优惠券详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("coupon:storeCoupon:query")
    @GetMapping("/{id}")
    public R<KxStoreCouponVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(kxStoreCouponService.queryById(id));
    }

    /**
     * 新增优惠券
     */
    @SaCheckPermission("coupon:storeCoupon:add")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KxStoreCouponBo bo) {
        if(CouponEnum.TYPE_1.getValue().equals(bo.getType())
            && ObjectUtils.isEmpty(bo.getCategoryId())){
            throw new ServiceException("请选择二级分类");
        }
        if(bo.getCouponPrice().compareTo(bo.getUseMinPrice()) > 0) {
            throw new ServiceException("优惠券金额不能高于最低消费金额");
        }
        return toAjax(kxStoreCouponService.insertByBo(bo));
    }

    /**
     * 修改优惠券
     */
    @SaCheckPermission("coupon:storeCoupon:edit")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KxStoreCouponBo bo) {
        if(CouponEnum.TYPE_1.getValue().equals(bo.getType())
            && ObjectUtils.isEmpty(bo.getCategoryId())){
            throw new ServiceException("请选择二级分类");
        }
        if(bo.getCouponPrice().compareTo(bo.getUseMinPrice()) > 0) {
            throw new ServiceException("优惠券金额不能高于最低消费金额");
        }
        return toAjax(kxStoreCouponService.updateByBo(bo));
    }

    /**
     * 删除优惠券
     *
     * @param ids 主键串
     */
    @SaCheckPermission("coupon:storeCoupon:remove")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(kxStoreCouponService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
